package org.i_plus.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.i_plus.Assistance;
import org.i_plus.model.MemberBean;
import org.i_plus.model.services.MemberService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class MatchServlet extends HttpServlet {
	private MemberService service;

	@Override
	public void init() throws ServletException {
		WebApplicationContext context =
				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
		this.service = (MemberService)context.getBean("MemberService");
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//接收資料	
				String region = req.getParameter("region");
				String sex =   req.getParameter("sex");
				String constellation =   req.getParameter("constellation");
				String temp1 =   req.getParameter("hight");
				String blood =   req.getParameter("blood");
				String mstate =   req.getParameter("mstate");
				String temp2 =   req.getParameter("play");
				String temp3[] =   req.getParameterValues("hobby");
				String page = req.getParameter("page");
				
				//把條件設定回去
				List<String> conditions = new ArrayList<String>();
				req.setAttribute("region", region);
				req.setAttribute("sex", sex);
				req.setAttribute("constellation", constellation);
				req.setAttribute("hight", temp1);
				req.setAttribute("blood", blood);
				req.setAttribute("mstate", mstate);
				req.setAttribute("play", temp2);
				String hobby_URL = "";
				
				
		//驗證資料
				Map<String, String> errors = new HashMap<String, String>();
				req.setAttribute("errors", errors);
				
				int hight = 0;
				if(temp1 != null && temp1.trim().length() !=0){
					try {
						hight = Integer.parseInt(temp1);
					} catch (NumberFormatException e) {
						errors.put("hight", "please input a number");
					}
				}
				if(errors != null && !errors.isEmpty()){
					req.getRequestDispatcher("/View/Match.jsp").forward(req, resp);
					return;
				}
		//轉換資料
				if("不限".equals(region))
					region = null;
				if("不限".equals(sex))
					sex = null;
				else
					sex = sex.substring(0);
				if("不限".equals(constellation))
					constellation = null;
				if("不限".equals(blood))
					blood = null;
				else if("AB".equals(blood))
					blood = "C";
				if("不限".equals(mstate))
					mstate = null;
				boolean play = false;
				if("不抽菸不喝酒".equals(temp2))
					play = true;
				boolean [] hobby = {false,false,false,false,false,false,false,false};
				if(temp3 != null)
				for(String temp:temp3){
					if("ball".equals(temp)){
						hobby[0] = true;
						hobby_URL += "&hobby=ball";
					}
					if("run".equals(temp)){
						hobby[1] = true;
						hobby_URL += "&hobby=run";
					}
					if("reading".equals(temp)){
						hobby[2] = true;
						hobby_URL += "&hobby=reading";
					}
					if("swim".equals(temp)){
						hobby[3] = true;
						hobby_URL += "&hobby=swim";
					}
					if("movie".equals(temp)){
						hobby[4] = true;
						hobby_URL += "&hobby=movie";
					}
					if("singing".equals(temp)){
						hobby[5] = true;
						hobby_URL += "&hobby=singing";
					}
					if("bike".equals(temp)){
						hobby[6] = true;
						hobby_URL += "&hobby=bike";
					}
					if("twgame".equals(temp)){
						hobby[7] = true;
						hobby_URL += "&hobby=twgame";
					}
				}
				if(page==null)
					page = "1";
				
				//把條件設定回去
				req.setAttribute("hobby", hobby_URL);
				
		//呼叫model 依照model回傳結果 呼叫view		
				List result = service.matchByProcedure(Integer.parseInt(page), region, sex, hight, blood, mstate, play, 
						hobby[0], hobby[1], hobby[2], hobby[3], hobby[4], hobby[5], hobby[6], hobby[7],constellation);
				
				List<String> resultpic = new ArrayList<String>();
				for(MemberBean temp:(List<MemberBean>)result.get(1))
					resultpic.add(Assistance.photoByteToImgString("image/jpeg", temp.getPic1()));
	

			
				int temp = (int)result.get(0);
				int pages[] = new int[temp];
				for(int i=1;i<=temp;i++)
					pages[i-1]=i;
				
				req.setAttribute("pages",pages );
				req.setAttribute("beans", result.get(1));
				req.setAttribute("beanspic",resultpic);
				resp.setCharacterEncoding("UTF-8");
				req.getRequestDispatcher("/View/MatchDisplay.jsp").forward(req, resp);
				return;
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	
}
